<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/common/head.jsp"%>
</head>
<body class="${cookie.bodyClass.value}">
	<div class="container body">
		<div class="main_container">
			<%@ include file="/WEB-INF/common/left.jsp"%>
			<%@ include file="/WEB-INF/common/top.jsp"%>
			<!-- page content -->
			<div class="right_col" role="main" id="main">
				<div class="x_panel">
					<div class="x_title">
						<h2>商品管理</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<form role="form" id="searchForm">
							<div class="container">
								<div class="row">
									<div class="col-lg-3">
										<input class="form-control input-sm" placeholder="商品名称" name="map['commodityName']"/>
									</div>
									<div class="col-lg-3">
										<select class="form-control input-sm"  name="map['isRecommend']" >
											<option value="">是否推荐</option>
											<option value="1">是</option>
											<option value="0">否</option>
										</select>
									</div>
									<div class="col-lg-3">
										<select class="form-control input-sm" name="map['isPointsExchange']">
											<option value="">是否是积分兑换商品</option>
											<option value="1">是</option>
											<option value="0">否</option>
										</select>
									</div>
									<div class="col-lg-3">
										<button type="button" class="btn btn-success btn-sm" onclick="add();">新增</button>
										<button type="button" class="btn btn-primary btn-sm" onclick="search();">搜索</button>
									</div>
								</div>
							</div>
						</form>
						<div class="table-responsive">
							<table id="commoditys" class="table table-bordered table-striped table-hover" 
							  cellspacing="0" width="100%">
								<thead>
									<tr class="mytableHead">
										<th class="myRowNumber"></th>
										<!-- <th>id</th> -->
										<th>图片</th>
										<th>商品名称</th>
										<th>商品分类</th>
										<th>商品价格</th>
										<th>商品运费</th>
										<th>商品销量</th>
										<th>是否上架</th>
										<th>是否推荐</th>
										<th>是否是积分兑换商品</th>
										<th>创建时间</th>
										<th>操作</th>
									</tr>
								</thead>
							</table>
						</div>
					</div>
				</div>
			</div>
			<!-- /page content -->
			<%@ include file="/WEB-INF/common/foot.jsp"%>
		</div>
	</div>
	<%@ include file="/WEB-INF/common/script.jsp"%>
	<script>
	$(function(){
		var t = $("#commoditys").DataTable({
		    ajax: {
		        url: ctx+'admin/commodity/findCommoditys',
		        type: 'POST',
		        data: function(d){$.extend(d,serializeObject($("#searchForm")))}
		    },
		    columns: [
		              	{ "data":null,"render":rowNumberRender},
				     /*    { "data":'commodityId'}, */
				        { "data":'imageUrl',"render":imageUrlRender},
				        { "data":'commodityName'},
				        { "data":'categoryName'},
				        { "data":'commodityPrice'},
				        { "data":'freight'},
				        { "data":'realBuyCount'},
				        { "data":'isShelves',"render":rowisShelves},
				        { "data":'isRecommend',"render":rowisRecommend},
				        { "data":'isPointsExchange',"render":rowisPointsExchange},
				        { "data":'createDate'},
				        { "data":'id',"render":optRender}
		    ]
		    
		});
		$("#searchForm input").keydown(function(event){
			if(event.keyCode == 13){
				search();
			}
		});
		$("#searchForm select").change(search);
	});
	
	function optRender(data, type, row, meta){
		var but = '<a class="btn btn-info btn-xs" href="javascript:view('+meta.row+')"><i class="fa fa-pencil"></i>编辑</a>';
		but = but + '<a class="btn btn-danger btn-xs" href="javascript:deleteById('+row.commodityId+')"><i class="fa fa-remove"></i>删除</a>';
		return but;
	}
	function add(){
		window.location = ctx+"admin/commodity/toCommodityDetailPage";
	} 
	function view(index,event){
		var row = $('#commoditys').DataTable().rows().data()[index];
		window.location = ctx+"admin/commodity/toCommodityDetailPage?commodityId="+row.commodityId
	}
	
	function rowNumberRender(data, type, row, meta) {
	    var setting = meta.settings;
	    var rowNumber = setting._iDisplayStart + (meta.row + 1);
		return rowNumber;
  	}
	
	function imageUrlRender(data, type, row, meta){
		var imageUrl=row.imageUrl;
		return "<img style='width:50px;height:50px' src='"+imgDomain+"/"+imageUrl+"'/>";
	}
	function rowisShelves(data, type, row, meta){
		var isShelves = row.isShelves;
		if(Number(isShelves)==0){
		    return '否';
		}else if(Number(isShelves)==1){
            return '是';
		}
	}

	function rowisRecommend(data, type, row, meta){
		var isRecommend = row.isRecommend;
		if(Number(isRecommend)==0){
		    return '否';
		}else if(Number(isRecommend)==1){
            return '是';
		}
	}

    function rowisPointsExchange(data, type, row, meta){
        var isPointsExchange = row.isPointsExchange;
        if(Number(isPointsExchange)==0){
            return '否';
        }else if(Number(isPointsExchange)==1){
            return '是';
        }
    }
	function search(){
		$('#commoditys').DataTable().ajax.reload(function(d){$.extend(d,serializeObject($("#searchForm")))}, true);
	}
	function deleteById(id){
		swal({ "title": "", "text": "确定要删除该商品" ,"type": "warning","showCancelButton":true},function(isConfirm){
			if(isConfirm){
				$.ajax({
					url: ctx+"admin/commodity/deleteCommodity?commodityId="+id+"&time="+(new Date()).getTime(),
					dataType: "json",
					success: function(result){
						if(result.status == "0"){
							search();
						}else{
							swal({title:'',text:result.msg,type:"error"});
						}
					}
				});
			}
		});
	}
	</script>
</body>
</html>